// pages/category/category.js
import {request} from '../../request/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    leftMenuList:[],
    rightContent:[],
    currentIndex:0,
    scrollTop:0
  },
  catelist:[],
  list:[],
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //先判断本地有没有数据
    const categories = wx.getStorageSync('cates');
    if(!categories){
      request({url:'/categories'})
    .then((value)=>{
      this.catelist = value.data.message;
      //
      wx.setStorageSync('cates', {time:Date.now(),date:this.catelist})
      let leftMenuList = this.catelist.map((c)=>{
        return c.cat_name;
      })
      this.list = this.catelist[0].children
      this.setData({
        rightContent:this.list
      })
      this.setData({
        leftMenuList
      })
    }).catch((error)=>{
      console.log(error.message);
    })
    }else{
      this.catelist = categories.date;
      let leftMenuList = this.catelist.map((c)=>{
        return c.cat_name;
      })
      this.list = this.catelist[0].children
      this.setData({
        rightContent:this.list
      })
      this.setData({
        leftMenuList
      })
    }
  

    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  menuClick(e){
    let a = e.currentTarget.dataset.index;
    let rightContent = this.catelist[a].children;
    this.setData({
      rightContent,
      currentIndex : a,
      scrollTop:0
    })
  }
})